<?php

namespace app\api\controller;

use app\api\model\ContactModel;
use app\api\model\ContactUsModel;
use app\api\validate\IndexValidate;
use app\BaseController;
use think\Exception;

class ContactUs extends BaseController
{
    public function index()
    {
        try {
            //接收数据
            $data = $this->request->param();
            //验证数据
            $validate = new IndexValidate();
            $result = $validate->scene('contactUs')->check($data);
            if ($result!== true) {
                $this->error($validate->getError());
            }
            //添加数据
            $model = new ContactUsModel();
            if ( $model->save($data)) {
                $this->success('留言成功');
            } else {
                $this->error('留言失败');
            }
        } catch (Exception $e) {
            $this->error('', $e->getMessage());
        }
    }
}